package com.pkdl.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.pkdl.dto.PhieuKham_DichVuDTO;

// Data access object, which get data of PhieuKham_DichVu table from database
public class PhieuKham_DichVuDAO {
	public boolean delete(int mapkdv) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_Delete(?)}");
			cs.setInt(1, mapkdv);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean deleteByMaPhieuKham(int mapk) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_DeleteByMaPhieuKham(?)}");
			cs.setInt(1, mapk);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean insert(PhieuKham_DichVuDTO phieukham_dichvu) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_Insert(?, ?, ?)}");
			cs.setInt(1, phieukham_dichvu.getMaPhieuKham());
			cs.setInt(2, phieukham_dichvu.getMaDichVu());
			cs.setString(3, phieukham_dichvu.getKetLuan());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public ArrayList<PhieuKham_DichVuDTO> selectAll() {
		ArrayList<PhieuKham_DichVuDTO> list = new ArrayList<PhieuKham_DichVuDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				PhieuKham_DichVuDTO phieukham_dichvu = new PhieuKham_DichVuDTO();
				phieukham_dichvu.setMaPKDV(rs.getInt("MaPKDV"));
				phieukham_dichvu.setMaPhieuKham(rs.getInt("MaPhieuKham"));
				phieukham_dichvu.setMaDichVu(rs.getInt("MaDichVu"));
				phieukham_dichvu.setKetLuan(rs.getString("KetLuan"));
				list.add(phieukham_dichvu);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public PhieuKham_DichVuDTO selectById(int mapkdv) {
		PhieuKham_DichVuDTO phieukham_dichvu = new PhieuKham_DichVuDTO();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_SelectByID(?)}");
			cs.setInt(1, mapkdv);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				phieukham_dichvu.setMaPKDV(rs.getInt("MaPKDV"));
				phieukham_dichvu.setMaPhieuKham(rs.getInt("MaPhieuKham"));
				phieukham_dichvu.setMaDichVu(rs.getInt("MaDichVu"));
				phieukham_dichvu.setKetLuan(rs.getString("KetLuan"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return phieukham_dichvu;
	}

	public ArrayList<PhieuKham_DichVuDTO> selectByMaDichVu(int madichvu) {
		ArrayList<PhieuKham_DichVuDTO> list = new ArrayList<PhieuKham_DichVuDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_SelectByMaDichVu(?)}");
			cs.setInt(1, madichvu);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				PhieuKham_DichVuDTO phieukham_dichvu = new PhieuKham_DichVuDTO();
				phieukham_dichvu.setMaPKDV(rs.getInt("MaPKDV"));
				phieukham_dichvu.setMaPhieuKham(rs.getInt("MaPhieuKham"));
				phieukham_dichvu.setMaDichVu(rs.getInt("MaDichVu"));
				phieukham_dichvu.setKetLuan(rs.getString("KetLuan"));
				list.add(phieukham_dichvu);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public ArrayList<PhieuKham_DichVuDTO> selectByMaNguoiKham(int manguoikham) {
		ArrayList<PhieuKham_DichVuDTO> list = new ArrayList<PhieuKham_DichVuDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_SelectByMaNguoiKham(?)}");
			cs.setInt(1, manguoikham);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				PhieuKham_DichVuDTO phieukham_dichvu = new PhieuKham_DichVuDTO();
				phieukham_dichvu.setMaPKDV(rs.getInt("MaPKDV"));
				phieukham_dichvu.setMaPhieuKham(rs.getInt("MaPhieuKham"));
				phieukham_dichvu.setMaDichVu(rs.getInt("MaDichVu"));
				phieukham_dichvu.setKetLuan(rs.getString("KetLuan"));
				list.add(phieukham_dichvu);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public ArrayList<PhieuKham_DichVuDTO> selectByMaPhieuKham(int maphieukham) {
		ArrayList<PhieuKham_DichVuDTO> list = new ArrayList<PhieuKham_DichVuDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_SelectByMaPhieuKham(?)}");
			cs.setInt(1, maphieukham);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				PhieuKham_DichVuDTO phieukham_dichvu = new PhieuKham_DichVuDTO();
				phieukham_dichvu.setMaPKDV(rs.getInt("MaPKDV"));
				phieukham_dichvu.setMaPhieuKham(maphieukham);
				phieukham_dichvu.setMaDichVu(rs.getInt("MaDichVu"));
				phieukham_dichvu.setTenDichVu(rs.getString("TenDichVu"));
				phieukham_dichvu.setKetLuan(rs.getString("KetLuan"));
				list.add(phieukham_dichvu);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public boolean update(PhieuKham_DichVuDTO phieukham_dichvu) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_PhieuKham_DichVu_Update(?, ?, ?, ?, ?, ?)}");
			cs.setInt(1, phieukham_dichvu.getMaPKDV());
			cs.setInt(2, phieukham_dichvu.getMaPhieuKham());
			cs.setInt(3, phieukham_dichvu.getMaDichVu());
			cs.setString(4, phieukham_dichvu.getKetLuan());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
					Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(PhieuKham_DichVuDAO.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}
		return result;
	}

}
